package com.zhu.app.dwd;


import com.zhu.utils.ZhuKafkaUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.ZoneId;

/**
 * 交易域订单取消事物事实表
 *
 */
public class DWDTradeCancelDetailApp {

    public static void main(String[] args) throws Exception {

        //todo env
        StreamExecutionEnvironment streamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
        streamExecutionEnvironment.setParallelism(1);  //kafka partition
        //checkPoint
       /*
        streamExecutionEnvironment.enableCheckpointing(5 * 60000L, CheckpointingMode.EXACTLY_ONCE); //精确一次

        //状态后端
        streamExecutionEnvironment.setStateBackend(new HashMapStateBackend());
        streamExecutionEnvironment.getCheckpointConfig().setCheckpointStorage(ClusterParametersConfig.HDFS_CHECKPOINT_FILE_DIR);  //检查点保存在hdfs
        System.setProperty("HADOOP_USER_NAME", "zhu");
        streamExecutionEnvironment.getCheckpointConfig().setCheckpointTimeout(10 * 60000L);  //TimeOut
        streamExecutionEnvironment.getCheckpointConfig().setMaxConcurrentCheckpoints(2);  //最大共存检查点
        streamExecutionEnvironment.setRestartStrategy(RestartStrategies.fixedDelayRestart(3, 5 * 1000L));  //重启策略
        */
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(streamExecutionEnvironment);
        tableEnvironment.getConfig().setLocalTimeZone(ZoneId.of("GMT+8"));

        //todo read kafka topic dwd_trade_order_pre_process
        tableEnvironment.executeSql("" +
                "create table dwd_trade_order_pre_process(\n" +
                "id string,\n" +
                "order_id string,\n" +
                "user_id string,\n" +
                "order_status string,\n" +
                "sku_id string,\n" +
                "sku_name string,\n" +
                "province_id string,\n" +
                "activity_id string,\n" +
                "activity_rule_id string,\n" +
                "coupon_id string,\n" +
                "date_id string,\n" +
                "create_time string,\n" +
                "operate_date_id string,\n" +
                "operate_time string,\n" +
                "source_id string,\n" +
                "source_type string,\n" +
                "source_type_name string,\n" +
                "sku_num string,\n" +
                "split_original_amount string,\n" +
                "split_activity_amount string,\n" +
                "split_coupon_amount string,\n" +
                "split_total_amount string,\n" +
                "`type` string,\n" +
                "`old` map<string,string>,\n" +
                "od_ts string,\n" +
                "oi_ts string,\n" +
                "row_op_ts timestamp_ltz(3)\n" +
                ")" + ZhuKafkaUtil.getKafkaDDL(
                "dwd_trade_order_pre_process",
                "dwd_trade_cancel_detail"));

        //todo 筛选订单取消数据
        Table filterCancelTable = tableEnvironment.sqlQuery(
                "select " +
                        "id, " +
                        "order_id, " +
                        "user_id, " +
                        "sku_id, " +
                        "sku_name, " +
                        "province_id, " +
                        "activity_id, " +
                        "activity_rule_id, " +
                        "coupon_id, " +
                        "operate_date_id date_id, " +
                        "operate_time cancel_time, " +  //取消订单的操作时间
                        "source_id, " +
                        "source_type source_type_code, " +
                        "source_type_name, " +
                        "sku_num, " +
                        "split_original_amount, " +
                        "split_activity_amount, " +
                        "split_coupon_amount, " +
                        "split_total_amount, " +
                        "row_op_ts " +
                        "from dwd_trade_order_pre_process " +
                        "where `type` = 'update'" +
                        "and `old`['order_status'] is not null " +
                        "and order_status = '1003'"   //取消订单切且上一步有操作
        );
        tableEnvironment.createTemporaryView("filter_cancel_table",filterCancelTable);

        //todo  建立写往kafka的外部.
        tableEnvironment.executeSql("create table dwd_trade_cancel_detail (\n" +
                        "id string,\n" +
                                "order_id string,\n" +
                                "user_id string,\n" +
                                "sku_id string,\n" +
                                "sku_name string,\n" +
                                "province_id string,\n" +
                                "activity_id string,\n" +
                                "activity_rule_id string,\n" +
                                "coupon_id string,\n" +
                                "date_id string,\n" +
                                "cancel_time string,\n" +
                                "source_id string,\n" +
                                "source_type_code string,\n" +
                                "source_type_name string,\n" +
                                "sku_num string,\n" +
                                "split_original_amount string,\n" +
                                "split_activity_amount string,\n" +
                                "split_coupon_amount string,\n" +
                                "split_total_amount string,\n" +
                                "row_op_ts timestamp_ltz(3)\n" +
                                ")" +
                                ZhuKafkaUtil.getKafkaSinkDDL("dwd_trade_cancel_detail"));

        //todo write to kafka topic dwd_trade_cancel_detail

        tableEnvironment.executeSql("insert into dwd_trade_cancel_detail " +
                "select * from filter_cancel_table").print();

        streamExecutionEnvironment.execute();

    }
}
